package com.handong.handserialprogram.utils;

import android.content.Context;
import com.amap.api.maps.AMap;
import com.amap.api.maps.CameraUpdateFactory;
import com.amap.api.maps.model.BitmapDescriptor;
import com.amap.api.maps.model.BitmapDescriptorFactory;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.Marker;
import com.amap.api.maps.model.MarkerOptions;
import com.amap.api.services.core.AMapException;
import com.amap.api.services.core.LatLonPoint;
import com.amap.api.services.geocoder.GeocodeSearch;
import com.amap.api.services.geocoder.RegeocodeQuery;

/**
 * Created by zhongqiu on 2019/10/4.
 */

public class AMapUtil {
    public static Marker addMarkerOnMap(AMap AMap, LatLng latLng, int resID) {
        if (AMap == null) {
            return null;
        }
        BitmapDescriptor descriptor = BitmapDescriptorFactory.fromResource(resID);
        MarkerOptions options = new MarkerOptions()
                .icon(descriptor)
                .position(latLng);
        Marker marker = (Marker) AMap.addMarker(options);
        return marker;
    }

    public static void locateToCenter(AMap aMap, double lnt, double lng) {
        LatLng latLng = new LatLng(lnt, lng);
        locateToCenter(aMap, latLng);
    }

    public static void locateToCenter(AMap map, LatLng latLng) {
        map.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng,17.0f));
    }

    /**
     * 定位到中心点。
     *
     * @param map    高德地图。
     * @param latLng 中心点。
     */
    public static void locateToCenterNoZoom(AMap map, LatLng latLng) {
        map.animateCamera(CameraUpdateFactory.newLatLng(latLng));
    }

    public static void latLngToAddress(Context context,LatLonPoint latLng, GeocodeSearch.OnGeocodeSearchListener listener) {
        GeocodeSearch geoCoder= null;
        try {
            geoCoder = new GeocodeSearch(context);
        } catch (AMapException e) {
            e.printStackTrace();
        }
        geoCoder.setOnGeocodeSearchListener(listener);
        geoCoder.getFromLocationAsyn(new RegeocodeQuery(latLng,200,GeocodeSearch.AMAP));

    }
}
